/* Jesse Phillips
 * CSCD 305 -- minor1
 * Compiler: g++
 */
#include "complex.h"
#include "complex_driver.h"

map<char, Complex> var;
int sign = 0;

int main(int argc, char **argv) {
	bool interp = false;
	bool help = false;
	cout << "Welcome to Jesse's Complex Number Calculator." << endl;
	if(argc > 1) {
		for(int i=1; i < argc; i++) {
			char *flag = argv[i];
			if(*flag == '-') {
				flag++;
				if(*flag == 'i')
					interp = true;
				else if(*flag == 'h')
					help = true;
			}else
				file(flag);
		}
	}

	if(!interp)
		cout << "Usage: " << argv[0] << " [option] [file]" << endl
		     << "Options: \n\t-i\tRun interpreter\n\t-h\tThis help" << endl;
	if(interp)
		interpreter();
	else if(!help)
		menu();

	return 0;
}
void menu() {
	int cmd = 1;
	while(cmd) {
		printMenu();
		cout << "Choice: ";
		cin >> cmd;
		switch (cmd) {
		case 1:
			interpreter();
			break;
		case 2:
			help();
			break;
		}

	}
}

void help() {
	cout << "This program operates like the python interpreter." << endl;
	cout << "Each complex number must be in a variable to use it." << endl;
	cout << "A variable is any lowercase or capital letter." << endl;
	cout << "\nThere are two ways to populate variable." << endl;
	cout << "\t1. Launching the program with a file parameter." << endl;
	cout << "\t\tOnly the first 6 numbers will be read in, the file does not need\n"
		<< "\t\tto end with a valid complex number." << endl;
	cout << "\t2. Through the Interpretor." << endl;
	cout << "\t\tNotation is var = complex i.e. (a = 4 +3i)" << endl;
	cout << "\t\tAll other notation is var sign var." << endl;

	cout << "Signs:\n\t# -- \tShow value of variable." << endl;
	cout << "\t= -- \tassign a Complex number to a variable." << endl;
	cout << "\t+,-,*\tPerform arithmetic expression. (Only between two variables)" << endl;
	cout << "\t? -- \tCheck if two variables are the same." << endl;
	cout << "\t! -- \tCheck if two variables are not the same." << endl;
	cout << "\t0 -- \tQuit command-line interpreter." << endl;
}

void printMenu() {
	cout << "1. Command-line Interpretor" << endl;
	cout << "2. Introduction" << endl;
	cout << "0. Quit\n" << endl;
}

void interpreter() {
	cout << "Expression structure: var sign (var OR Complex)." << endl;
	cout << "Variables are letters from a-Z." << endl;
	cout << "? to get help." << endl;
	char a, b, c;
	int r, i;
	Complex foo;
	Complex bar;
	for(;;) {
		sign = 0;
		cout << ">>> ";
		cin >> a;
		if(a == ')') cin >> a;
		if(a == '0') break;
		if(a == '?') {
			help();
			continue;
		}
		if(a == '(')
			cin >> foo;
		else
			foo = var[a];
		cin >> c;
		if(c == ')')
			cin >> c;
		switch (c) {
		case '*':
			sign = 1;
		case '-':
			sign++;
		case '+':
			sign++;
			cin >> b;
			if(b == '(')
				cin >> bar;
			else
				bar = var[b];
			cout << "(" << foo << ") " << c << " (" << bar << ") =" << endl;
			solve(foo, sign, bar, cout);
			break;
		case '!':
			sign++;
		case '?':
			sign += equals;
			cin >> b;
			if(b == '(') {
				cin >> bar;
			}else
				bar = var[b];
			cout << "(" << foo << ") " << c << "= " << " (" << bar << ") =" << endl;
			solve(foo, sign, bar, cout);
			break;
		case '#':
			sign = other;
			cout << foo << endl;
			break;
		case '=':
			sign = other;
			cin >> bar;
			var[a] = bar;
			break;
		}
	}
}

void solve(Complex a, int sign, Complex b, ostream& out) {
	switch(sign) {
	case add:
		out << a + b << endl;
		break;
	case subtract:
		out << a - b << endl;
		break;
	case multiply:
		out << a * b << endl;
		break;
	case equals:
		out << ((a == b) ? "True": "False") << endl;
		break;
	case notEquals:
		out << ((a != b) ? "True": "False") << endl;
		break;
	default:
		out << "oops shouldn't be here." << endl;
	}
}

void file(char* input) {
	char invert[z];
	int i;
	char key = 'a';
	ifstream fin;
	Complex foo;

	fin.open(input, ios::in );

	if(fin) {
		while((key < 'z') && (fin >> foo)) {
			var[key++] = foo;
		}
	}else {
		cerr << "Could not open file " << input << endl;
	}
}


